﻿ // 开启监听Alt + 2
// 关闭监听Alt + 3
// 开启后可以复制、粘贴时，把\号改为/号。

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle(QStringLiteral("Qt之剪切板、热键应用"));

    QClipboard *m_pClipboard = QApplication::clipboard();   //获取系统剪贴板指针
    connect(m_pClipboard, &QClipboard::changed, this, &MainWindow::on_ClipboardChanged);

    if (!RegisterHotKey(HWND(winId()), HOT_KEY_CTRL_2, MOD_ALT, 0x32))// MOD_ALT | MOD_CONTROL
        qDebug() << "注册热键 ALT + 2";
    if (!RegisterHotKey(HWND(winId()), HOT_KEY_CTRL_3, MOD_ALT, 0x33))
        qDebug() << "注册热键 ALT + 3";
    //
    m_pTimer = new QTimer(this);
    m_pTimer->setSingleShot(false);
    connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_ClipboardAdd);
    m_pTimer->start(10);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);
    MSG* msg = static_cast<MSG*>(message);
    if (msg->message == WM_HOTKEY)
    {
        switch (msg->wParam) {
        case HOT_KEY_CTRL_2:
            qDebug() << "触发了： ALT + 2";
            m_bSwitch = true;
            break;
        case HOT_KEY_CTRL_3:
            qDebug() << "触发了： ALT + 3";
            m_bSwitch = false;
            break;
        default:
            qDebug() << "被注入了其他热键.";
        }
        return true;
    }
    return false;
}

void MainWindow::on_ClipboardChanged()
{
    const QMimeData *mimeData = m_pClipboard->mimeData();
    if (mimeData->hasImage())
    {
        /* 获取剪切板图像数据。 */
        QPixmap pixmap = qvariant_cast<QPixmap>(mimeData->imageData());
        //
    }
    else if (mimeData->hasHtml())
    {
        /* 获取剪切板html数据。 */
        QString html = mimeData->html();
        //
    }
    else if (mimeData->hasText()) {
        /* 获取剪切板纯文本数据。 */
        QString text = mimeData->text();
        //
        if (m_bSwitch)
        {
            text = text.replace("\\", "/");
            ui->textEdit->setText(text);
            m_bReadFinish = true;
            m_Txt = text;
        }
    }
    else {
        qDebug() << "Cannot display data";
    }
}

void MainWindow::on_ClipboardAdd()
{
    if (m_bReadFinish && m_Txt != "")
    {
        m_bReadFinish = false;
        m_pClipboard->setText(m_Txt);
    }
}

void MainWindow::on_pushButton_clicked()
{
    m_bSwitch = true;
}

void MainWindow::on_pushButton_2_clicked()
{
    m_bSwitch = false;
}

void MainWindow::on_pushButton_3_clicked()
{
    m_pClipboard->setText("AAA");
}

void MainWindow::on_pushButton_6_clicked()
{
    ui->textEdit->setText(m_pClipboard->text());
}

void MainWindow::on_pushButton_4_clicked()
{
    QMimeData mimeData;
    mimeData.setImageData(QPixmap("hello.png")); // 图像数据
    // mimeData.setHtml("<h1>Heool world.</h1>"); // Html数据
    // mimeData.setText("Hello world."); // 纯文本数据
    m_pClipboard->setMimeData(&mimeData);
}
